/* This file prepares Google ASVI data. We downloaded the data at daily frequency over 200 day periods from Google trends, then calculated the median SVI over a 5 week period from t-65 to t-35 days --> ASVI_{k,t} = SVI_{k,t} / MedSVI_{k,t-56 --> t-35}  following Niessner (2015) (but without the week scaling which is unneeded). To concatenate the 200 day periods, we ensure an overlap day at the end of the older period (data A) and at the beginning of the newer period (data B), and scale the new 200 day data by A/B. This makes the new 200 days have the same scale as the old. */

/* 
Input files: ASVI_final.csv
Output files: gASVI_data
*/



**********************************************
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
cd `=maindir'
**********************************************

import delimited ASVI_final.csv, clear  

foreach var of varlist _all {  
    capture assert missing(`var')
    if !_rc {
        drop `var'
    }
}
ren date dd
g int date=date(dd,"MDY")
format date %td
drop dd

rename (eurcad-zyne) asvi= 
reshape long asvi, i(date) j(ticker_crsp) string


replace asvi=ln(1+asvi)
label var asvi "ln(1+Google ASVI)"
replace ticker = upper(ticker)
replace ticker="WTW" if ticker=="WW"

replace asvi=0 if mi(asvi) 

egen id= group(ticker), label 
tsset id date
forval k=1/5 {
gen asvi_l`k'=l`k'.asvi
replace asvi_l`k'=0 if mi(asvi_l`k')
}
g asvi_l5day=asvi_l1 + asvi_l2 + asvi_l3 + asvi_l4 + asvi_l5
	label var asvi_l5day "LnGoogle_ASVI t-5 to t-1 inclusive"
	drop asvi_l1-asvi_l5 id


sort date ticker
save gASVI_data, replace

